package com.cqw.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-04-13 11:26
 * @Description: 时间工具类
 */
public class DateUtil {


    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    private static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String DEFAULT_DATE_FORMAT_ORDER = "yyyyMMdd";


    /**
     * 获取年月日（当天）
     *
     * @return
     */
    public static String getDay() {
        LocalDate now = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
        return fmt.format(now);
    }


    /**
     * 获取年月日时分秒（当天，不包含毫秒）
     *
     * @return
     */
    public static String getDayTime() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT);
        return fmt.format(now);
    }


    /**
     * date类型转换字符串
     *
     * @param date
     * @return
     */
    public static String getDateToString(Date date) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT_ORDER);
        return fmt.format(localDateTime);
    }


    /**
     * localDate转字符串
     *
     * @param localDate
     * @return
     */
    public static String getLocalDateToString(LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT_ORDER);
        return fmt.format(localDate);
    }


    /**
     * localDateTime转字符串
     *
     * @param localDateTime
     * @return
     */
    public static String getLocalDateToString(LocalDateTime localDateTime) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT);
        return fmt.format(localDateTime);
    }


    /**
     * 自定固定时间
     *
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static LocalDateTime getFixedTime(int year, int month, int day, int hour, int minute, int second) {
        return LocalDateTime.of(year, month, day, hour, minute, second);
    }


    /**
     * 设置固定时间
     *
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static LocalDateTime settingsTime(int year, int month, int day, int hour, int minute, int second) {
        return LocalDateTime.now().withYear(year).withMonth(month).withDayOfMonth(day).withHour(hour).withMinute(minute).withSecond(second);
    }


    /**
     * 当月第一天
     *
     * @return
     */
    public static LocalDate firstToMonth() {
        LocalDate localDate = LocalDate.now();
        return localDate.with(TemporalAdjusters.firstDayOfMonth());
    }


    /**
     * 本月的最后一天
     *
     * @return
     */
    public static LocalDate lastToMonth() {
        LocalDate now = LocalDate.now();
        return now.with(TemporalAdjusters.lastDayOfMonth());
    }


    /**
     * 下个月第一天
     *
     * @return
     */
    public static LocalDate firstNextMonth() {
        LocalDate localDate = LocalDate.now();
        return localDate.with(TemporalAdjusters.firstDayOfNextMonth());
    }


    /**
     * 本年第一天
     *
     * @return
     */
    public static LocalDate firstToYear() {
        LocalDate localDate = LocalDate.now();
        return localDate.with(TemporalAdjusters.firstDayOfYear());
    }


    /**
     * 本年最后一天
     *
     * @return
     */
    public static LocalDate lastToYear() {
        LocalDate localDate = LocalDate.now();
        return localDate.with(TemporalAdjusters.lastDayOfYear());
    }


    /**
     * 相差多少秒
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long difference(LocalDateTime time1, LocalDateTime time2) {
        return Duration.between(time1, time2).getSeconds();
    }


    /**
     * 相差多少年
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewYearsApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.YEARS.between(time1, time2);
    }


    /**
     * 相差多少月
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewMonthsApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.MONTHS.between(time1, time2);
    }


    /**
     * 相差多少周
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewWeeksApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.WEEKS.between(time1, time2);
    }


    /**
     * 相差多少小时
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewHoursApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.HOURS.between(time1, time2);
    }


    /**
     * 相差多少分钟
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewMinutesApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.MINUTES.between(time1, time2);
    }


    /**
     * 相差多少秒
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewSecondsApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.SECONDS.between(time1, time2);
    }


    /**
     * 相差多少毫秒
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewMillisApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.MILLIS.between(time1, time2);
    }


    /**
     * 相差多少纳秒
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long aFewNanosApart(LocalDateTime time1, LocalDateTime time2) {
        return ChronoUnit.NANOS.between(time1, time2);
    }


    /**
     * 获取时间戳-13位
     *
     * @return
     */
    public static long getTimesTamp() {
        return Clock.systemDefaultZone().millis();
    }


    /**
     * date转LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    /**
     * date转localDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }


    /**
     * date转localTime
     *
     * @param date
     * @return
     */
    public static LocalTime dateToLocalTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }


    /**
     * localDateTime转date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }


    /**
     * localDate转date，时分秒毫秒默认为0
     *
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }


    /**
     * 字符串转LocalDate
     *
     * @param str
     * @return
     */
    public static LocalDate stringToLocalDate(String str) {
        return LocalDate.parse(str, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
    }


    /**
     * 字符串转LocalDateTime
     *
     * @param str
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String str) {
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
    }


    /**
     * 时间戳转LocalDateTime
     *
     * @param timeTamp
     * @return
     */
    public static LocalDateTime timeTampToLocalDateTime(long timeTamp) {
        Instant instant = Instant.ofEpochMilli(timeTamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }


    /**
     * LocalDateTime转时间戳
     *
     * @param localDateTime
     * @return
     */
    public static long LocalDateTimeToTimeTamp(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }


    /**
     * 获取周几
     *
     * @param localDate
     * @return
     */
    public static String getWeek(LocalDate localDate) {
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        String week = String.valueOf(dayOfWeek);
        switch (week) {
            case "MONDAY":
                week = "周一";
                break;
            case "TUESDAY":
                week = "周二";
                break;
            case "WEDNESDAY":
                week = "周三";
                break;
            case "THURSDAY":
                week = "周四";
                break;
            case "FRIDAY":
                week = "周五";
                break;
            case "SATURDAY":
                week = "周六";
                break;
            case "SUNDAY":
                week = "周日";
                break;
        }
        return week;
    }


    public static void main(String[] args) {
        LocalDateTime fixedTime1 = getFixedTime(2004, 1, 3, 4, 4, 5);
        LocalDateTime fixedTime2 = getFixedTime(2004, 1, 3, 4, 5, 5);

        String fixedTime = getWeek(LocalDate.now());
        System.out.println(fixedTime);
    }


}
